<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.core.dao.${relation.upperFirstTableKey}Dao">
	<resultMap id="${relation.upperFirstTableKey}" type="${package}.core.model.${relation.upperFirstTableKey}">
		<#list relation.table.columns as column>
		<#if (column.primary) >
		<id property="${column.key}" column="${column.name}" jdbcType="${genCodeUtil.getColumnJdbcType(column)}"/>
		<#else>
		<result property="${column.key}" column="${column.name}" jdbcType="${genCodeUtil.getColumnJdbcType(column)}"/>
		</#if>
		</#list>
	</resultMap>
	
	<insert id="create" parameterType="${package}.core.model.${relation.upperFirstTableKey}">
		INSERT INTO ${relation.table.name}
		(<#list relation.table.columns as column>${column.name}<#if column_has_next>,</#if></#list>)
		VALUES 
		(<#list relation.table.columns as column><#noparse>#{</#noparse>${column.key},jdbcType=${genCodeUtil.getColumnJdbcType(column)}<#noparse>}</#noparse><#if column_has_next>, </#if></#list>)
	</insert>
	
	<select id="get"   parameterType="java.lang.String" resultMap="${relation.upperFirstTableKey}">
		SELECT * FROM ${relation.table.name} 
		WHERE 
		${relation.table.getPkName()}=<#noparse>#{</#noparse>${relation.table.getPkKey()}}
	</select>
	
	<select id="query" parameterType="java.util.Map" resultMap="${relation.upperFirstTableKey}">
		SELECT * FROM ${relation.table.name}
		<where>
			<if test="whereSql!=null">
				<#noparse>${</#noparse>whereSql}
			</if>
		</where>
		<if test="orderBySql!=null">
			ORDER BY <#noparse>${</#noparse>orderBySql}
		</if>
		<if test="orderBySql==null">
			ORDER BY ${relation.table.getPkName()} DESC
		</if>
	</select>
	
	<update id="update" parameterType="${package}.core.model.${relation.upperFirstTableKey}">
		UPDATE ${relation.table.name} SET
		<#list commonList as col>
		${column.name}=<#noparse>#{</#noparse>${column.key},jdbcType=${genCodeUtil.getColumnJdbcType(column)}<#noparse>}</#noparse><#if column_has_next>,</#if>
		</#list>
		WHERE
		${relation.table.getPkName()}=<#noparse>#{</#noparse>${relation.table.getPkKey()}}
	</update>
	
	<delete id="remove" parameterType="java.lang.String">
		DELETE FROM ${relation.table.name} 
		WHERE
		${relation.table.getPkName()}=<#noparse>#{</#noparse>${relation.table.getPkKey()}}
	</delete>
	
	<#if relation.parent??>
	<delete id="delBy${genCodeUtil.getUpperFk(relation,true)}">
	    DELETE FROM ${relation.table.name}
	    WHERE
	    ${genCodeUtil.getFkDbColumnName(relation)}=<#noparse>#{</#noparse>${genCodeUtil.getFkName(relation,true)}}
	</delete>    
	
	<select id="getBy${genCodeUtil.getUpperFk(relation,true)}" resultMap="${relation.upperFirstTableKey}">
	    SELECT *
	    FROM ${relation.table.name} 
	    WHERE ${genCodeUtil.getFkDbColumnName(relation)}=<#noparse>#{</#noparse>${genCodeUtil.getFkName(relation,true)}}
	</select>
	</#if>
	
	<insert id="bulkCreate" parameterType="${package}.core.model.${relation.upperFirstTableKey}" databaseId="mysql">
		INSERT INTO ${relation.table.name}
		(<#list relation.table.columns as column>${column.name}<#if column_has_next>,</#if></#list>)
		VALUES 
		<foreach collection="list" item="item" index="index" separator=","> 
			(<#list relation.table.columns as column><#noparse>#{</#noparse>item.${column.key},jdbcType=${genCodeUtil.getColumnJdbcType(column)}<#noparse>}</#noparse><#if column_has_next>, </#if></#list>)
		</foreach>
	</insert>
	
	<delete id="bulkRemove" parameterType="java.lang.String">
		DELETE FROM ${relation.table.name}
		WHERE id_ in 
		<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">  	<#noparse>#{</#noparse>id}  </foreach>
	</delete>

	
</mapper>